define(['moment'], function (moment) {
    return ['$scope', '$rootScope', '$mdDialog', 'MMWService', '$filter', '$stateParams', '$timeout', 'config', '$calculater', '$gallery',
        function ($scope, $rootScope, $mdDialog, $MMWService, $filter, $stateParams, $timeout, config, $calculater, $gallery) {

            // 流程
            $scope.phases = [
                'process_tool',
                'process_maintain_item'
            ];

            $scope.init = function(){
                $scope.user_name = config.cache.name;
                $scope.envInfo = $filter('translate')('Login.env.'+config.setting.environment);
                $scope.item_list = [];
                // 預設流程狀態
                $scope.phases_index = 0;

                // 自動隱蔽鍵盤
                element_barcode_no = document.getElementById('input_barcode');
                makeBarcodeInput(element_barcode_no);
                element_barcode_no.focus();
            };

            $scope.back = function(){
                $scope.$root.hideMenu();
                $scope.$root.changeProgram('menu', true);
            };

            // QRCode輸入
            $scope.QRCodeScanner = function(){
                $scope.$root.OpenScanner({
                    after : function(result){
                        input_barcode_no_callback(result.text);
                    },
                    exception: function(){
                        //目前裝置無法使用相機
                        $scope.$root.showAlert($filter('translate')('common.msg.cant_use_camera'));
                    },
                    orientation : 'portrait'
                });
            };

            // barcode輸入
            $scope.input_barcode = function(barcode){
                input_barcode_no_callback(barcode);
            };

            // 流程狀態
            $scope.PhaseStatus = function(index){
                if($scope.phases_index > index)
                    return ['prev'];
                else if($scope.phases_index == index)
                    return ['now'];
                else
                    return ['next'];
            };

            // 回上一步流程
            $scope.BackToPhase = function (index){
                if($scope.phases_index > index){
                    $scope.phases_index = index;
                    if(index == 0){
                        $scope.tool_class = '';
                        $scope.tool_no = '';
                        $scope.tool_state = '';
                        $scope.last_check_date = '';
                        $scope.life_count = '';
                        $scope.acc_count = '';
                        $scope.usage_count = '';
                        $scope.actual_count = '';

                        $scope.item_list = [];
                    };
                };
                element_barcode_no.focus();
            };

            // barcode輸入 callback
            function input_barcode_no_callback(barcode){
                if(barcode != ''){
                    // 工具流程
                    tool_info_get({
                        tool_no: barcode
                    }, function(data){
                        if(data.state =='0' || data.state =='70' || data.state =='31'){//0:在庫、70:維修、31清洗完成
                            $scope.phases_index = 1;
                            $scope.tool_class = data.tool_class;
                            $scope.tool_no = data.tool_no;
                            $scope.tool_state = data.state;
                            $scope.last_check_date = data.last_check_date;
                            $scope.life_count = data.life_count;
                            $scope.acc_count = data.acc_count;
                            $scope.usage_count = data.usage_count;
                            $scope.actual_count = data.actual_count;
                            $scope.tool_type_no = data.tool_type_no;
                        }else{
                            $rootScope.showAlert($filter('translate')('SMT10.msg.not_in_house_or_maintain'));
                        }
                    });
                };
            };

            // 點擊開始檢測按鈕
            $scope.startDetect = function(){
                if($scope.phases_index == 1){
                    var content = {
                        cmlist_type: 'C',
                        tool_class: $scope.tool_class,
                        tool_type_no: $scope.tool_type_no,
                        tool_no: $scope.tool_no
                    };
                    tool_checklist_info_get(content, function(data){
                        if(data.check_list && data.check_list.length > 0){
                            $scope.item_list = data.check_list;
                        } else {
                            $scope.$root.showAlert($filter('translate')('SMT10.msg.check_list_empty'));
                        }
                    });
                };
            };

            // 點擊確定按鈕執行完成
            $scope.confirm = function(){
                var content = {
                    cmlist_type: 'C',
                    cmlist_no:$scope.cmlist_no,
                    tool_class: $scope.tool_class,
                    tool_type_no: $scope.tool_type_no,
                    tool_no: $scope.tool_no,
                    check_list: $scope.item_list
                };
                tool_checklist_info_set_process(content, function(data){
                    $scope.phases_index = 0;
                    $scope.tool_class = '';
                    $scope.tool_no = '';
                    $scope.tool_state = '';
                    $scope.last_check_date = '';
                    $scope.life_count = '';
                    $scope.acc_count = '';
                    $scope.usage_count = '';
                    $scope.actual_count = '';
                    $scope.item_list = [];
                    $scope.$root.showToast($filter('translate')('SMT10.msg.success'));
                });
            };

            // 計算機
            $scope.changeValue = function(event, item){
                var target = event.currentTarget;
                angular.element(target).addClass('is-focus');
                $calculater.open(function(num,dialog){
                    angular.element(target).removeClass('is-focus');
                    item.input_value = (+num);
                    if(item.input_value >= (+item.min_value) && item.input_value <= (+item.max_value)){
                        item.is_ok = '1';
                    } else {
                        item.is_ok = '0';
                    }
                    dialog.hide();
                },
                function(dialog){
                    angular.element(target).removeClass('is-focus');
                    dialog.hide();
                },
                {
                    defaultNum : item.input_value,
                    decimal : true
                });
            };

            // 滑動按鈕
            $scope.switchValue = function(item){
                if(item.input_value == '1'){
                    item.status = item.input_value = '0';
                } else {
                    item.status = item.input_value = '1';
                };
            };

            // 圖片
            $scope.open_checkPic = function(item){
                $gallery.show(item.sop_path, '', function(){});
            };

            // 取回 工具資訊
            function tool_info_get(content, callback){
                $MMWService.sendToServer({
                    uri : 'ServicesSMT.Module_SMT.tool_info_get',
                    content : content,
                    success : function(data){
                        if(callback)
                            callback(data);
                    }
                });
            };

            // 取得 維護項目資料
            function tool_checklist_info_get(content, callback){
                $MMWService.sendToServer({
                    uri : 'ServicesSMT.Module_SMT.tool_checklist_info_get',
                    content : content,
                    success : function(data){
                        if(callback)
                            callback(data);
                    }
                });
            };

            // 送回 維護項目資料
            function tool_checklist_info_set_process(content, callback){
                $MMWService.sendToServer({
                    uri : 'ServicesSMT.Module_SMT.tool_checklist_info_set_process',
                    content : content,
                    success : function(data){
                        if(callback)
                            callback(data);
                    }
                });
            };

            $scope.input_PDA_barcode = function(event){
                if (event.keyCode == 13) {
                    element_barcode_no.blur();
                    input_barcode_no_callback($scope.barcode)
                    $scope.barcode = '';
                } else {
                    $scope.barcode = $scope.barcode || '';
                }
            }

            //20200914 modify by Dustdusk for M#: 增加
            function makeBarcodeInput(el) {
                var _input = el.getElementsByClassName('barcode_input')[0];
                //var _mask = el.getElementsByClassName('barcode_mask')[0];
                el.focus = function () {
                    //e.preventDefault();
                    //_mask.click();
                    el.click();
                }
                el.addEventListener('click', function (e) {
                    e.preventDefault();
                    //_input.value = '';
                    if (document.activeElement !== _input)
                        _input.focus();
                });
                _input.addEventListener('focus', function (e) {
                    e.preventDefault();
                    setTimeout(function () {
                        _input.removeAttribute('readonly');
                    }, 200);
                });
                _input.addEventListener('blur', function (e) {
                    e.preventDefault();
                    _input.setAttribute('readonly', 'readonly');
                });
            }
        }
    ];
});
